package com.wzh.house.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wzh.common.utils.JwtUtil;
import com.wzh.house.service.RentUserAttentionService;
import com.wzh.house.util.OssUtils;
import com.wzh.house.entity.RentImgEntity;
import com.wzh.house.service.RentImgService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wzh.house.entity.RentHouseEntity;
import com.wzh.house.service.RentHouseService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author wzh
 * @email 1281204121@qq.com
 * @date 2022-03-22 19:04:16
 */
@RestController
@RequestMapping("house/renthouse")
public class RentHouseController {
    @Autowired
    private RentHouseService rentHouseService;
    @Autowired
    private OssUtils ossUtils;
    @Autowired
    private RentImgService rentImgService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private RentUserAttentionService rentUserAttentionService;

//    /*
//    * 置顶操作
//    * */
//    @RequestMapping("/upup/{houseId}")
//    public R upup(@PathVariable("houseId")Integer houseId){
//        //首先根据id获取房子信息
//        return rentHouseService.getById1(houseId);
//    }


    /**
     * 根据id查
     */
    @RequestMapping("/MyRentHouseList")
    public List<RentHouseEntity> MyRentHouseList(@RequestParam("userId")Integer userId){
        List<RentHouseEntity> list=rentHouseService.listById(userId);
        return list;

    }

    @RequestMapping("/MyRentHouseList2")
    public List<RentHouseEntity> MyRentHouseList2(){

        return rentHouseService.ListDesc();
    }

    /**
     * 我的出租房列表
     */
    @RequestMapping("/myRentList")
    public R myRentList(@RequestParam Map<String, Object> params,HttpServletRequest request){

        String token = request.getHeader("token");
        if (token==null){
            return R.error("用户未登录");
        }
        Claims claims = jwtUtil.parseJWT(token);
        if (claims==null||!"user".equals(claims.get("roles"))){
            return R.error("用户未登录");
        }
        Integer user_id = Integer.parseInt(claims.getId());
        PageUtils page = rentHouseService.queryPage1(params,user_id);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/rentHouseList")
    public List<RentHouseEntity> rentHouseList(){
        return rentHouseService.list();

    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = rentHouseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{houseId}")
    public R info(@PathVariable("houseId") Integer houseId, HttpServletRequest request){
        RentHouseEntity rentHouse = rentHouseService.getById(houseId);

        String token = request.getHeader("token");
        if (token!=null) {
            Claims claims = jwtUtil.parseJWT(token);
            if (claims!=null&&"user".equals(claims.get("roles"))) {
                Integer i = rentUserAttentionService.getCountByRentAndUserId(houseId,Integer.valueOf(claims.getId()));
                if (i>0){
                    rentHouse.setAttention(true);
                }
            }
        }
        return R.ok().put("rentHouse", rentHouse).setData(rentHouse);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody RentHouseEntity rentHouse){
        //,1.jpeg,2.jpeg
        String[] split = rentHouse.getDefaultImg().substring(1).split(",");
        rentHouse.setDefaultImg(split[0]);//默认图片为第一张
        boolean save = rentHouseService.save(rentHouse);
        if (save){
            //添加图片到图片表里 批量添加
            ArrayList<RentImgEntity> rentImgEntities = new ArrayList<>();
            for (String s : split) {
                RentImgEntity rentImgEntity = new RentImgEntity();
                rentImgEntity.setHouseId(rentHouse.getHouseId());
                rentImgEntity.setImgUrl(s);
                if (s.equals(split[0])){
                    rentImgEntity.setImgDefault(0);//是否为默认(0.是1.否)
                }else {
                    rentImgEntity.setImgDefault(1);
                }
                rentImgEntities.add(rentImgEntity);
            }
            rentImgService.saveBatch(rentImgEntities);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody RentHouseEntity rentHouse){
        //删除图片表里之前的图片 批删
        rentImgService.removeByHouseId(rentHouse.getHouseId());
        //,1.jpeg,2.jpeg
        String[] split = rentHouse.getDefaultImg().substring(1).split(",");
        rentHouse.setDefaultImg(split[0]);//默认图片为第一张
        boolean b = rentHouseService.updateById(rentHouse);
        if (b){
            //添加图片到图片表里 批量添加
            ArrayList<RentImgEntity> rentImgEntities = new ArrayList<>();
            for (String s : split) {
                RentImgEntity rentImgEntity = new RentImgEntity();
                rentImgEntity.setHouseId(rentHouse.getHouseId());
                rentImgEntity.setImgUrl(s);
                if (s.equals(split[0])){
                    rentImgEntity.setImgDefault(0);//是否为默认(0.是1.否)
                }else {
                    rentImgEntity.setImgDefault(1);
                }
                rentImgEntities.add(rentImgEntity);
            }
            rentImgService.saveBatch(rentImgEntities);
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] houseIds){
		rentHouseService.removeByIds(Arrays.asList(houseIds));

        return R.ok();
    }

    /**
     * 图片上传
     */
    @RequestMapping("/upload")
    public R upload(MultipartFile file){
        R r = ossUtils.uploadFile(file);
        return r;
    }


    /**
     * 修改
     */
    @RequestMapping("/updateZd")
    public R updateZd(@RequestBody RentHouseEntity rentHouseEntity){
        rentHouseService.updateById(rentHouseEntity);

        return R.ok();
    }

    @RequestMapping("/selectBySort")
    public List<RentHouseEntity> selectBySort(){
        return rentHouseService.selectBySort();
    }

}
